// SPDX-License-Identifier: GPL-3.0-or-later

import QtQuick
import Qt5Compat.GraphicalEffects

import Fk.Widgets as W

Item {
  id: root
  property bool hovered: false
  property bool enabled: true
  property alias source: image.source
  signal clicked
  signal rightClicked

  Image {
    id: image
    height: parent.height
    width: sourceSize.width * height / sourceSize.height
    anchors.left: parent.left
    fillMode: Image.PreserveAspectFit
  }

  W.TapHandler {
    id: mouse
    acceptedButtons: Qt.LeftButton | Qt.RightButton | Qt.NoButton
    gesturePolicy: TapHandler.WithinBounds

    onTapped: (p, btn) => {
      if (parent.enabled) {
        if (btn === Qt.LeftButton || btn === Qt.NoButton) {
          parent.clicked();
        } else if (btn === Qt.RightButton) {
          parent.rightClicked();
        }
      }
    }

    onLongPressed: {
      parent.rightClicked();
    }
  }

  BrightnessContrast {
    anchors.fill: parent
    source: parent
    brightness: parent.hovered ? 0.3 : 0
    visible: root.enabled && parent.hovered
  }

  HoverHandler {
    id: hover
    cursorShape: root.enabled ? Qt.PointingHandCursor : Qt.ArrowCursor
    onHoveredChanged: {
      if (hovered && root.enabled) {
        root.hovered = true;
      } else {
        root.hovered = false;
      }
    }
  }

}